package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.CheckGroupDao;
import com.itheima.dao.CheckItemDao;
import com.itheima.dao.SetmealDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import freemarker.template.Configuration;
import freemarker.template.Template;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfig;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = SetmealService.class)
@Transactional
public class SetmealServiceImpl implements SetmealService {

    //自动装配
    @Autowired
    private SetmealDao setmealDao;


    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        //分页设置
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        Page<Setmeal> page = setmealDao.findPage(queryPageBean.getQueryString());
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public void add(Setmeal setmeal, Integer[] checkgroupIds) {
        //添加检查套餐的数据=>要回封装id到对象中
        setmealDao.add(setmeal);
        //添加中间标的数据
        setSetmealIdAndCheckgroupIds(setmeal.getId(),checkgroupIds);

        //当天加元素时,更新并创建所有的静态资源文件
        getSetmealHtml();
    }

    @Autowired
    private FreeMarkerConfig freeMarkerConfig;

    //生成的静态资源文件的地址
    @Value("${out_put_path}")
    private String outPutPath;


    //更新并创建所有的静态资源文件
    public void getSetmealHtml() {
        //获得所有的setmeal元素(只有setmeal元素不包含checkGroup等元素)
        List<Setmeal> list = findAll();

        //创建setmeal的静态资源
        getMobileSetmealHtml(list);

        //创建mobile_setmeal_detail静态资源
        getMobileSetmealDetailHtml(list);
    }

    //创建mobile_setmeal_detail静态资源
    public void getMobileSetmealDetailHtml(List<Setmeal> list) {

        HashMap<String,Object> map = new HashMap<>();
        map.put("setmealList",list);

        generteHtml("mobile_setmeal.ftl","setmeal01.html",map);
    }

    //创建setmeal的静态资源
    public void getMobileSetmealHtml(List<Setmeal> list) {
        HashMap<String,Object> map = new HashMap<>();

        for (Setmeal setmeal : list) {

            Setmeal setmeal1 = findById(setmeal.getId());
            map.put("setmeal",setmeal1);

            generteHtml("mobile_setmeal_detail.ftl","setmeal_detail"+setmeal.getId()+".html",map);
        }

    }

    //创建静态资源文件的方法
    public void generteHtml(String templateName,String htmlPageName ,Map map){

        //获得模板对象(配置文件中已经配置好模板地址)
        Configuration configuration = freeMarkerConfig.getConfiguration();
        //创建输出流,把页面创建到指定的位置
        Writer out = null;
        try {
            //获得模板对象(参数为模板名字)
            Template template = configuration.getTemplate(templateName);
            out = new FileWriter(new File(outPutPath+"/"+htmlPageName));
            template.process(map,out);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if(out != null){
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }


    }

    //查询全部预约套餐
    @Override
    public List<Setmeal> findAll() {
        return setmealDao.findAll();
    }

    @Autowired
    private CheckGroupDao checkGroupDao;

    @Autowired
    private CheckItemDao checkItemDao;

    //查询套餐数据
    @Override
    public Setmeal findById(Integer id) {
        //根据id查询当前预约套餐数据
        Setmeal setmeal = setmealDao.findById(id);
        //跟据套餐的id查询它所有的项目组的数据
        List<CheckGroup> checkGroups = checkGroupDao.findCheckGroupsBySetmealId(id);
        //遍历
        for (CheckGroup checkGroup : checkGroups) {
            //根据checkGroup的id查询所有的checkItems的数据
            List<CheckItem> checkItems = checkItemDao.findCheckItemsByCheckGroupId(checkGroup.getId());
            checkGroup.setCheckItems(checkItems);
        }
        setmeal.setCheckGroups(checkGroups);
        return setmeal;
    }

    //仅查询setmeal
    @Override
    public Setmeal findOneById(Integer id) {
        return setmealDao.findById(id);
    }

    //获取套餐统计数据
    @Override
    public Map<String, Object> getSetmealReport() {
        //从数据库中获取数据
       List<Map<String ,Object>> list = setmealDao.getSetmealCountByOrder();
        //填写
       Map<String, Object> map = new HashMap<>();
       List<String> setmealNames = new ArrayList<>();
       for(Map map1:list){
           String name = (String) map1.get("name");
           setmealNames.add(name);
       }
       map.put("setmealCount",list);
       map.put("setmealNames",setmealNames);
       return map;
    }

    private void setSetmealIdAndCheckgroupIds(Integer setmealId, Integer[] checkgroupIds) {
        //遍历
        for (Integer checkgroupId : checkgroupIds) {
            Map<String, Integer> map = new HashMap<>();
            map.put("setmealId",setmealId);
            map.put("checkgroupId",checkgroupId);
            setmealDao.setSetmealIdAndCheckgroupId(map);
        }
    }
}
